const app = getApp();
import grace from "../../../utils/grace.js";

var wxpay = require('../../../utils/pay.js');
const util = require('../../../utils/util.js');

grace.page({
    /**
     * 页面的初始数据
     */
    data: {
        orderId: 0,
        amountTotal: 0,
        commission: 0,
        order: {},
        role: '',
        can_refund: false
    },
    onLoad(e) {
        this.$data.orderId = e.id;
        this.$data.role = wx.getStorageSync('role');
    },
    onShow() {
        let that = this;
        wx.showLoading({
            title: '加载中...',
        })
        let url = app.globalData.api + '/api/v1.0/orders/' + that.$data.orderId;
        that.$http.get(url, {token: wx.getStorageSync('token')}).then((res) => {
            wx.hideLoading();
            if (res.data.code != 0) {
                wx.showModal({
                    title: '错误',
                    content: res.data.msg,
                    showCancel: false
                })
                return;
            } else {
                that.$data = {
                    order: res.data.data,
                    amountTotal: res.data.data.amount_total,
                    commission: res.data.data.commission
                };
            }
        }).catch(err => {
            console.log(err.status, err.message);
        })
    },
    paymentBtnTap(e) {
        let that = this;
        wx.showModal({
            title: '您确认支付吗？',
            content: '',
            success: function (res) {
                if (res.confirm) {
                    wxpay.wxpay(app, that.$data.amountTotal + that.$data.commission, that.$data.orderId, "/pages/mall/order/detail?id=" + that.$data.orderId);
                    that.onShow();
                }
            }
        })
    }
})
